/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "separator.h"

static const char class_name [] = "EGUI Widget Separator";

static int afc_easygui_internal_separator_get_template ( DynamicClass * dyn );

static int afc_easygui_internal_cb_separator ( DynamicClass * dyn );
static int afc_easygui_internal_separator_create_separator ( EasyGUIContext * data, short vertical, DynamicClass * dc );
static int afc_easygui_internal_separator_add_toolbar ( EasyGUIContext * data, DynamicClass * dc );
static int afc_easygui_internal_separator_add_menu ( EasyGUIContext * data, DynamicClass * dc );

DynamicClass * afc_easygui_internal_separator_new_instance ()
{
	DynamicClass * dyn = NULL;

	if ( ( dyn = afc_dynamic_class_new () ) == NULL ) 
	{
		AFC_LOG_FAST ( AFC_ERR_NO_MEMORY );
		return ( NULL );
	}

	afc_dynamic_class_add_method ( dyn, "get_template", NULL,  afc_easygui_internal_separator_get_template );
	afc_dynamic_class_add_method ( dyn, "open_callback", NULL,  afc_easygui_internal_cb_separator );
	afc_dynamic_class_add_method ( dyn, "enabled",	NULL, 	afc_easygui_widget_method_enabled );

	return ( dyn );
}

int afc_easygui_internal_separator_del_instance ( DynamicClass * dyn )
{
	return ( afc_easygui_widget_standard_free ( dyn ) );
	/*
	if ( dyn != NULL ) afc_dynamic_class_delete ( dyn );

	return ( AFC_ERR_NO_ERROR );
	*/
}

static int afc_easygui_internal_separator_get_template ( DynamicClass * dyn )
{
	dyn->result = "";
	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_cb_separator ( DynamicClass * dyn )
{
	EasyGUI * egui = dyn->info;
	EasyGUIContext * context = egui->curr_context;
	EasyGUIWidgetData * container;
	int afc_res = AFC_ERR_NO_ERROR;
	DynamicClass * dc;

	if ( context->curr_container == NULL ) return ( AFC_EASYGUI_ERR_NO_CONTAINER );

	if ( ( dc = afc_dynamic_class_master_new_instance ( egui->commands, "separator" ) ) == NULL )
		return ( AFC_LOG_FAST_INFO ( AFC_ERR_NO_MEMORY, "dynclass instance" ) );

	WIDGET_SET_CONTEXT ( dc, context );

	// We need to work on the current container
	container = context->curr_container;

	afc_easygui_widget_parse_default_params ( egui );


	switch ( container->type )
	{
		case AFC_EASYGUI_CONTAINER_TYPE_WINDOW:
		case AFC_EASYGUI_CONTAINER_TYPE_ROW:
			afc_res = afc_easygui_internal_separator_create_separator ( context, FALSE, dc );
			break;

		case AFC_EASYGUI_CONTAINER_TYPE_COLUMN:
			afc_res = afc_easygui_internal_separator_create_separator ( context, TRUE, dc );
			break;

		case AFC_EASYGUI_CONTAINER_TYPE_TOOLBAR:
			afc_res = afc_easygui_internal_separator_add_toolbar ( context, dc );
			break;

		case AFC_EASYGUI_CONTAINER_TYPE_MENU:
			afc_res = afc_easygui_internal_separator_add_menu ( context, dc );
			break;
	}

	return ( afc_res );
}

// Creates a "standard" button
static int afc_easygui_internal_separator_create_separator ( EasyGUIContext * context, short vertical, DynamicClass * dc )
{
	int afc_res;
	GtkWidget * w;


	if ( vertical )
		w = gtk_vseparator_new ();
	else
		w = gtk_hseparator_new ();

	/* first try to add the widget to the current container, then, if successful, set the other attributes */
	if ( ( afc_res = afc_easygui_internal_add_widget ( context, w, dc ) ) != AFC_ERR_NO_ERROR ) return ( afc_res );

	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_separator_add_toolbar ( EasyGUIContext * context, DynamicClass * dc )
{
	gtk_toolbar_append_space ( GTK_TOOLBAR ( context->curr_container->gtk_handle ) );

	afc_easygui_widget_register ( context, NULL, dc );

	return ( AFC_ERR_NO_ERROR );
}

static int afc_easygui_internal_separator_add_menu ( EasyGUIContext * context, DynamicClass * dc )
{
	afc_dynamic_class_execute ( context->curr_container->dc, "add", gtk_separator_menu_item_new (), AFC_DYNAMIC_CLASS_ARG_END );

	afc_easygui_widget_register ( context, NULL, dc );

	return ( AFC_ERR_NO_ERROR );
}
